package com.lnsystem.tools;

import java.util.List;

import org.apache.log4j.Logger;
import org.joda.time.LocalDate;
import org.joda.time.LocalDateTime;
import  org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;

import com.lnsystem.dao.StockDao;
import com.lnsystem.model.Stock;
import com.lnsystem.model.StockDailyData;
import com.sun.xml.internal.ws.policy.privateutil.PolicyUtils.Collections;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:beans.xml")

public class UpdatePriceActionForStockDaily {

	
	private Logger logger = Logger.getLogger(UpdatePriceActionForStockDaily.class);
	
	@Autowired 
	private  StockDao stockDao;
	
	//@Transactional
	public  void updatePriceAction() {
		List<Stock> allStocks = stockDao.getAllStocks();
		for(Stock stock : allStocks) {
			updatePriceAction(stock,null);
		}
	}
	
	
	@Transactional
	public String getPriceAction(String symbol, LocalDate date) {
		
		Stock stock = stockDao.getStockBySymbol(symbol);
		List<StockDailyData> dailyDataList = stockDao.getStockDailyData(stock, date.minusDays(1), date, false);
		updatePriceAction(stock,dailyDataList);
		return "";
	}

	@Transactional
	public void updatePriceAction(Stock stock, List<StockDailyData> dailyDataList) {

		double open;
		double close;
		//double high;
		//double low;
		StockDailyData today;
		StockDailyData yesterday = null;
		
		logger.info(">>>>> Now updating price action info for :"+stock.getSymbol()+"...");
		
		//List<Stock> allStocks = stockDao.getAllStocks();
		//for(Stock stock : allStocks) {
		if(dailyDataList == null) {
			dailyDataList = stockDao.getStockDailyData(stock, true);
			java.util.Collections.reverse(dailyDataList);
		}
			
		for(int i=0; i<dailyDataList.size(); i++) {
			
			today = dailyDataList.get(i);
			
			open = today.getOpen();
			//high = today.getHigh();
			//low = today.getLow();
			close = today.getClose();
			
			if(i == 0) { // first record - so price action is set based on just today's data.
				if(close >= open) {
					today.setPriceAction("G"); //green day
				}
				if(close < open) {
					today.setPriceAction("R"); //red day
				}
				yesterday = today;
			} else { // there is previous data available. so set price action based on prev day's data.
				
				//yesterday = dailyDataList.get(i-1);
				if( (close > open) && (close > yesterday.getClose())){ //today's open> today's close && today's close > y'days close.
					today.setPriceAction("G"); //green day
				} else if ((close > open) && (close < yesterday.getClose())){ //today's open> today's close && today's close > y'days close.
					today.setPriceAction("H"); //red hollow candle reversal day 
				} else if( (close < open) && (close > yesterday.getClose())) {
					today.setPriceAction("B"); //black or solid candle day as stock closed lower than opening price but above yesterday's close. 
				} else if ((close < open) && (close < yesterday.getClose()) ) {
					today.setPriceAction("R"); //red day
				}
				
				if( (close == open) && (close > yesterday.getClose())){ //today's open> today's close && today's close > y'days close.
					today.setPriceAction("G"); //green indecision day
				} else if ((close == open) && (close < yesterday.getClose())){ //today's open> today's close && today's close > y'days close.
					today.setPriceAction("R"); //red indecision day 
				} else if( (close > open) && (close == yesterday.getClose())) {
					today.setPriceAction("G"); //Green day as stock closed lower than opening price but above yesterday's close. 
				} else if ((close < open) && (close == yesterday.getClose()) ) {
					today.setPriceAction("R"); //red day
				}	else if ((close == open) && (close == yesterday.getClose()) ) {
					today.setPriceAction("G"); //neutral/green day
				}
				yesterday = today;
			}
			logger.debug(">>>>> Setting price action as :<<< "+today.getPriceAction()+" >>>>> for :"+stock.getSymbol()+" for ["+today.getDate()+"]");
		
		}
		//}
		java.util.Collections.reverse(dailyDataList);
		//persist price action changes..
		stockDao.addStock(stock);
		
		stockDao.flushAndClear();

	}
	

	
	@Test
	@Transactional
	@Rollback(false)
	@Ignore
	public void testUpdatePrieActionForOneStock() {
		 //Stock stock = stockDao.getStockBySymbol("LNKD");
		 //getPriceAction("LNKD",new LocalDate(LocalDateTime.now().minusDays(18)));
		Stock stock = stockDao.getStockBySymbol("LNKD");
		updatePriceAction(stock, null);
		Assert.assertTrue("test".contains("t"));
	}
	@Test
	@Transactional
	@Rollback(false)	
	public void testUpdatePriceActionForAllStocks() {
		updatePriceAction();
		Assert.assertTrue("test".contains("t"));
	}
/*	public static void main(String[] args) {
		
		ApplicationContext context = new ClassPathXmlApplicationContext("classpath:beans.xml");
	    StockDao stockDao = (StockDao) context.getBean("stockDaoHibernateImpl");
		Stock stock = stockDao.getStockBySymbol("LNKD");
		updatePrieAction(stock, null);
	}*/
}
